﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonTagTime;
using CommonTagTime.PagerControl;
using CommonTagTime.ModelObject;
using CommonTagTime.Business;
using System.Resources;

public partial class Shop : SitePage
{

    #region Init,Loads
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            LoadListShop();
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        InitData();
    }
    #endregion

    #region Methods
    private void InitData()
    {
        this.pagerTop.PageIndex = this.pagerBottom.PageIndex = 1;
        this.pagerTop.PageSize = this.pagerBottom.PageSize = clsConfigData.DEFAULT_SHOP_PAGE_SIZE;
        this.Page.Header.Title = string.Format(Resources.Resource.TITLE, "Shop");

    }

    private void LoadListShop()
    {
        int catePara = clsQueryString.GetInt("cateId");
        int countryId = clsQueryString.GetInt("countryId");
        DistributorCollection lstShop = new DistributorCollection();
        busDistributor objBus = new busDistributor();
        //get Condition
        CommonTagTime.ModelObject.Distributor objDistributor = new CommonTagTime.ModelObject.Distributor();
        objDistributor.CurrentPage = pagerTop.PageIndex;
        objDistributor.OrderBy = busDistributor.COMPANYNAME;
        objDistributor.OrderDirection = "DESC";
        objDistributor.PageSize = pagerTop.PageSize;
        objDistributor.Condition = "s.IsActive=1 ";
        if (countryId > 0)
        {
            busCountry objBusCountry = new busCountry();
            Country objCountry = objBusCountry.GetById(countryId);
            objDistributor.Condition = objDistributor.Condition + " and s.CountryCode='" + objCountry.CountryCode + "'";
        }
        else if (catePara > 0)
        {
            //get All country Code in this cate
            string countryCode = GetAllCountryCodeForCate(catePara);
            if (countryCode != string.Empty)
                countryCode = countryCode.Substring(0, countryCode.Length - 1);
            objDistributor.Condition = objDistributor.Condition + " and s.CountryCode in(" + countryCode + ")";
            LoadMetaData(catePara);
        }
        int count = 0;
        lstShop = objBus.List(objDistributor);
        if (lstShop != null && lstShop.Count > 0)
        {
            count = lstShop.Count;
            dalShop.DataSource = lstShop;
            dalShop.DataBind();
            pagerTop.SetPaging(count, objDistributor.TotalRecords);
            pagerBottom.SetPaging(count, objDistributor.TotalRecords);
            pagerTop.Visible = true;
            pagerBottom.Visible = true;
        }
        else
        {
            pagerTop.Visible = false;
            pagerBottom.Visible = false;
        }
    }
    private void LoadMetaData(int cateId)
    {
        busCategory objBusCate = new busCategory();
        Category objCate = objBusCate.GetByID(cateId);
        string title = string.Empty;
        if (objCate != null)
        {
            if (objCate.PageTitle == string.Empty)
                title = objCate.Name;
            else title = objCate.PageTitle;
            this.SetMetaData(string.Format(Resources.Resource.TITLE, Server.HtmlEncode(title)), objCate.MetaDescription, objCate.MetaKeywords);
        }

    }
    private string GetAllCountryCodeForCate(int catePara)
    {
        string result = string.Empty;
        CategoryCollection lstChild = LoadChild(catePara);
        if (lstChild != null && lstChild.Count > 0)
        {

            foreach (Category objCate in lstChild)
            {
                GetAllCountryCodeForCate(objCate.CategoryID);
            }
        }
        else
        {
            busCountryAreaCategory objBusCountryCate = new busCountryAreaCategory();
            CountryAreaCategoryCollection lstCate = objBusCountryCate.GetByCategoryId(catePara);
            if (lstCate != null && lstCate.Count > 0)
            {
                busCountry objBusCountry = new busCountry();
                foreach (CountryAreaCategory objCountryAreaCategory in lstCate)
                {
                   Country objCountry=objBusCountry.GetById(objCountryAreaCategory.CountryID);
                   if (objCountry != null)
                       result += "'" +  objCountry.CountryCode + "',";
                }
            }            
        }
        return result;
    }
    private CategoryCollection LoadChild(int parentId)
    {
        busCategory objBus = new busCategory();
        return objBus.GetByParentId(parentId);
    }
    #endregion

    #region Events
    protected void dalProduct_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        DataListItem item = e.Item;
        CommonTagTime.ModelObject.Distributor data = new CommonTagTime.ModelObject.Distributor();
        if ((item.ItemType == ListItemType.Item) || (item.ItemType == ListItemType.AlternatingItem))
        {
            if (data == null)
                return;
            data = (CommonTagTime.ModelObject.Distributor)item.DataItem;
            PlaceHolder phdControl = (PlaceHolder)item.FindControl("phdControlShop");
            UserControl ctrl = new UserControl();
            ucShopItem uc = (ucShopItem)ctrl.LoadControl("~/UserControl/ShopItem.ascx");
            if (uc != null)
                uc.objDistributor = data;
            phdControl.Controls.Add(uc);
        }
    }
    protected void pagerTop_PageIndexChanging(object obj, CommonTagTime.PagerControl.PageIndexChangeEventArgs e)
    {
        this.pagerTop.PageIndex = this.pagerBottom.PageIndex = e.PageIndex;
        LoadListShop();
    }
    protected void pagerTop_PageSizeChanging(object obj, CommonTagTime.PagerControl.PageSizeChangeEventArgs e)
    {
        this.pagerTop.PageIndex = this.pagerBottom.PageIndex = 1;
        this.pagerTop.PageSize = this.pagerBottom.PageSize = e.PageSize;
        LoadListShop();
    }
    #endregion
}